import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards, UsePipes } from '@nestjs/common';
import { RoleService } from './role.service';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import {
  CreateRoleDto,
  UpdateRoleDto,
  createRoleSchema,
  updateRoleSchema,
  paramSchema,
  roleMenuAuthSchema,
  roleDeptAuthSchema,
  RoleMenuAuthDto,
  RoleDeptAuthDto,
  queryRoleSchema,
  QueryRoleDto
} from './dto/role.schema';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

@ApiTags('role-角色模块')
@Controller('roles')
@UseGuards(JwtAuthGuard)
export class RoleController {
  constructor(private readonly roleService: RoleService) { }

  @Post()
  @ApiOperation({ summary: '新增角色' })
  @UsePipes(new ZodValidationPipe(createRoleSchema))
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }


  @Get()
  @ApiOperation({ summary: '查询所有角色' })
  findAll(@Query(new ZodValidationPipe(queryRoleSchema)) query: QueryRoleDto) {
    return this.roleService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '根据id查询角色' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  findOne(@Param() params: { id: string }) {
    return this.roleService.findOne(+params.id);
  }

  @Put(':id')
  @ApiOperation({ summary: '根据id更新角色' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  update(
    @Param() params: { id: string },
    @Body(new ZodValidationPipe(updateRoleSchema)) updateRoleDto: UpdateRoleDto,
  ) {
    return this.roleService.update(+params.id, updateRoleDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '根据id删除角色' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  remove(@Param() params: { id: string }) {
    return this.roleService.remove(+params.id);
  }

  @Post('authorize-menu')
  @ApiOperation({ summary: '根据id授权菜单' })
  @UsePipes(new ZodValidationPipe(roleMenuAuthSchema))
  authorizeMenu(@Body() authDto: RoleMenuAuthDto) {
    return this.roleService.authorizeMenu(authDto);
  }

  @Post('authorize-dept')
  @ApiOperation({ summary: '根据id授权部门' })
  @UsePipes(new ZodValidationPipe(roleDeptAuthSchema))
  authorizeDept(@Body() authDto: RoleDeptAuthDto) {
    return this.roleService.authorizeDept(authDto);
  }

  @Get('user/:userId')
  @ApiOperation({ summary: '根据用户id查询角色' })
  getUserRoles(@Param('userId') userId: string) {
    return this.roleService.getRolesByUserId(+userId);
  }
}